****APPENDIX FIGURE F1****

cd "$main_dir/output/figures/appendix_f"

graph set window fontface "Palatino"

//--------------------------------------------------------
//Create relevant program
//--------------------------------------------------------

scalar define age = 99

capture program drop lifeexp_lambda
program define lifeexp_lambda
	
	args var lambda
	forvalues i = 1/`=scalar(age)' {
	gen weight_`i' = (`i')^`lambda' if age>`i' & age!=-99
	}

	forvalues i = 1/`=scalar(age)' {
	gen weighted_`i' = weight_`i'*`var'_`i'
	}
	
	egen sum_weights_1 = rowtotal(weight_1-weight_`=scalar(age)'), missing
	
	if `lambda'<0 {
		
		if mod(`lambda', 1)==0 {
			local l = -1*`lambda'
			egen w`var'_neg`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_neg`l' = w`var'_neg`l'/sum_weights_1
		}
		else {
			local lambda = -1*`lambda'
			local l = round((1-(ceil(`lambda')-`lambda'))*10)
			local k = floor(`lambda')
			egen w`var'_neg`k'_`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_neg`k'_`l' = w`var'_neg`k'_`l'/sum_weights_1
		}
	} 
	else {
		if mod(`lambda', 1)==0 {
			egen w`var'_`lambda' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_`lambda' = w`var'_`lambda'/sum_weights_1
		}
		else {
			local l = round((1-(ceil(`lambda')-`lambda'))*10)
			local k = floor(`lambda')
			egen w`var'_`k'_`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_`k'_`l' = w`var'_`k'_`l'/sum_weights_1
		}
	}
	
	egen id = group(yob)
	keep id survey_year yob weighted_*	

end

//--------------------------------------------------------
//Create Figure F1
//--------------------------------------------------------

//lambda = -1
clear
use "$data_dir/raw/experience_datasets/growth_rate_mpd_exp.dta"

keep if ccode=="USA" & survey_year==2019

lifeexp_lambda growth_rate -1

reshape long weighted_, i(id) j(time_to_survey)
gen age = survey_year-yob
replace time_to_survey = age-time_to_survey
drop if weighted_==.

line weighted_ time_to_survey if yob==1969, graphregion(color(white)) title("{&lambda}=-1", color(black) size(large)) yline(0, lpattern(shortdash) lcolor(maroon)) ///
	ytitle("Weighted Growth Realizations", margin(small))  ylabel(-0.01(0.01)0.02) ///
	xtitle("Years Before Survey", margin(small))
graph export "figure_f1_a.png", replace

//lambda = 1
clear
use "$data_dir/raw/experience_datasets/growth_rate_mpd_exp.dta"

keep if ccode=="USA" & survey_year==2019

lifeexp_lambda growth_rate 1

reshape long weighted_, i(id) j(time_to_survey)
gen age = survey_year-yob
replace time_to_survey = age-time_to_survey
drop if weighted_==.

line weighted_ time_to_survey if yob==1969, graphregion(color(white)) title("{&lambda}=1", color(black) size(large)) yline(0, lpattern(shortdash) lcolor(maroon)) ///
	ytitle("Weighted Growth Realizations", margin(small))  ylabel(-1.5(1)2) ///
	xtitle("Years Before Survey", margin(small))
graph export "figure_f1_b.png", replace

//lambda = 2
clear
use "$data_dir/raw/experience_datasets/growth_rate_mpd_exp.dta"

keep if ccode=="USA" & survey_year==2019

lifeexp_lambda growth_rate 2

reshape long weighted_, i(id) j(time_to_survey)
gen age = survey_year-yob
replace time_to_survey = age-time_to_survey
drop if weighted_==.

line weighted_ time_to_survey if yob==1969, graphregion(color(white)) title("{&lambda}=2", color(black) size(large)) yline(0, lpattern(shortdash) lcolor(maroon)) ///
	ytitle("Weighted Growth Realizations", margin(small)) ylabel(-50(50)100) ///
	xtitle("Years Before Survey", margin(small))
graph export "figure_f1_c.png", replace
